package com.sam.im.heiqun.uis.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.FriendHeadEntity;
import com.sam.im.heiqun.entities.GroupFriendEntivity;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.ImGroupEntivity;
import com.sam.im.heiqun.entities.MessageEntivity;
import com.sam.im.heiqun.uis.adapters.holder.NewFriendHolder;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.utils.GlideUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Objects;

/**
 * Created by Joe on 2016/12/25.
 */

public class GroupTransAdpter extends RecyclerView.Adapter<NewFriendHolder> implements View.OnClickListener {

    private static final String TAG = GroupTransAdpter.class.getSimpleName();
    private Context context;
    private List<MessageEntivity> mGroupEntivities;
    private NewFriendAdpter.NewFriendListClickListener mItemClickListener;


    public GroupTransAdpter(Context context, List<MessageEntivity> groupEntivities) {
        this.context = context;
        this.mGroupEntivities = groupEntivities;
    }

    public GroupTransAdpter(Context context) {
        this.context = context;
    }

    public void refresh(List<MessageEntivity> groupEntivities) {
        this.mGroupEntivities = groupEntivities;
        notifyDataSetChanged();
        int size = mGroupEntivities.size();
        for (int i = 0; i < size; i++) {
            MessageEntivity entivity = mGroupEntivities.get(i);
            entivity.setMessageNum(0);
            entivity.save();
        }
        EventBus.getDefault().post(Constant.BLACK_MESSAGE_REFRESH);
    }


    public void setItemClickListener(NewFriendAdpter.NewFriendListClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    @Override
    public NewFriendHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_new_friend, parent, false);
        return new NewFriendHolder(view, mItemClickListener);
    }

    @Override
    public void onBindViewHolder(NewFriendHolder holder, int position) {//这个需要查找用户信息
        MessageEntivity messageEntivity = mGroupEntivities.get(position);

        if (messageEntivity.getType() == Constant.MESSAGE_TYPR_GROUP_TRANS_ME ){
//        ImFriendEntivity imFriendEntivity = ImFriendEntivity.findById(ImFriendEntivity.class, messageEntivity.getDestid());
            ImGroupEntivity mImGroupEntivity = ImGroupEntivity.findById(ImGroupEntivity.class, messageEntivity.getDestid());
            System.out.print("sss"+mImGroupEntivity);

            if (null!=mImGroupEntivity && (""+mImGroupEntivity.getCreaterId()).equals(ToolsUtils.getMyUserId())){
                holder.unagree.setVisibility(View.GONE);
                holder.agree.setText(R.string.already_agree);
                holder.agree.setEnabled(false);
                holder.agree.setAlpha(0.5f);
            }else{
                holder.agree.setText(R.string.agree);
                holder.agree.setAlpha(1.0f);
                holder.unagree.setVisibility(View.VISIBLE);
            }
            //已拒绝
            Long messageid=messageEntivity.getId();
            final SharedPreferences sharedPreferences=context.getSharedPreferences("TransferThePossessionOf",Activity.MODE_PRIVATE);
            Long messageid1 = sharedPreferences.getLong(messageid+"messageid",0);

            //Long accept=sharedPreferences.getLong("accept",0);
            if (messageid1==2){
                holder.agree.setVisibility(View.GONE);
                holder.unagree.setText(R.string.apply_unagree);
                holder.unagree.setEnabled(false);
                holder.unagree.setAlpha(0.5f);
            }else {
                holder.unagree.setText(R.string.apply_refuse);
                holder.unagree.setAlpha(1.0f);
                holder.agree.setVisibility(View.VISIBLE);
            }

            JSONObject jhson = JSON.parseObject(messageEntivity.getContent());
            String name = jhson.getString("groupName");
            String groupId = jhson.getString("groupId");

            holder.nick.setText(context.getResources().getString(R.string.notice_group));
            if (name != null && !"".equals(name)) {
//            GlideUtils.loadCircleImage(context, friendHeadEntity.getHeadUrl(), holder.head_image);
                holder.content.setText(context.getResources().getString(R.string.group_z) + name + context.getResources().getString(R.string.is_zhuan_you));
            } else {
                holder.content.setText(context.getResources().getString(R.string.group_z) +groupId+ context.getResources().getString(R.string.is_zhuan_you));
            }
            holder.content.setVisibility(View.VISIBLE);
            if (messageEntivity.getImgUrl() != null && !messageEntivity.getImgUrl().equals("")) {
                GlideUtils.loadCircleImage(context, messageEntivity.getImgUrl(), holder.head_image);
            }
            holder.agree.setTag(position);
            holder.agree.setOnClickListener(this);
            holder.unagree.setTag(position);
            holder.unagree.setOnClickListener(this);
        }else if (messageEntivity.getType() == Constant.MESSAGE_GROUP_ADD_APPLY){

            //已拒绝
            Long messageid=messageEntivity.getId();
            final SharedPreferences sharedPreferences=context.getSharedPreferences("TransferThePossessionOf1",Activity.MODE_PRIVATE);
            Long messageid1 = sharedPreferences.getLong(messageid+"messageid",0);

            //Long accept=sharedPreferences.getLong("accept",0);
            if (messageid1==3){
                holder.agree.setVisibility(View.GONE);
                holder.unagree.setText(R.string.apply_unagree);
                holder.unagree.setEnabled(false);
                holder.unagree.setAlpha(0.5f);
            }else {
                holder.unagree.setText(R.string.apply_refuse);
                holder.unagree.setAlpha(1.0f);
                holder.agree.setVisibility(View.VISIBLE);
            }

            Long messageid2=sharedPreferences.getLong(messageid+"messageid1",0);
            //Long accept=sharedPreferences.getLong("accept",0);
            if (messageid2==4){
                holder.unagree.setVisibility(View.GONE);
                holder.agree.setText(R.string.already_agree);
                holder.agree.setEnabled(false);
                holder.agree.setAlpha(0.5f);
            }else{
                holder.agree.setText(R.string.agree);
                holder.agree.setAlpha(1.0f);
                holder.unagree.setVisibility(View.VISIBLE);
            }


           /* //0=未同意；1=已同意；2=已拒绝
            if (messageEntivity.getIsreceive() == 0){
                holder.agree.setText(R.string.agree);
                holder.agree.setAlpha(1.0f);
                holder.unagree.setVisibility(View.VISIBLE);
            }else if (messageEntivity.getIsreceive() == 1){
                holder.unagree.setVisibility(View.GONE);
                holder.agree.setText(R.string.already_agree);
                holder.agree.setEnabled(false);
                holder.agree.setAlpha(0.5f);
            }else if (messageEntivity.getIsreceive() == 2){
                holder.unagree.setVisibility(View.GONE);
                holder.agree.setText("已拒绝");
                holder.agree.setEnabled(false);
                holder.agree.setAlpha(0.5f);
            }*/
            Log.i(TAG, "onBindViewHolder: ============messageEntivity.getContent()========"+messageEntivity.getContent());
            try {
                String content = messageEntivity.getContent();
                if ((content.startsWith("\"") && content.endsWith("\"")) || (content.startsWith("'") && content.endsWith("'"))){
                    content = content.substring(1,content.length() - 1);
                }
                Log.i(TAG, "onBindViewHolder: ============messageEntivity.getContent()========"+content);

                JSONObject jhson = JSON.parseObject(content);
                String name = null!=jhson?jhson.getString("groupName"):"";
                String groupId = null!=jhson?jhson.getString("groupId"):"";
                String userAnswer = null!=jhson?jhson.getString("userAnswer"):"";
                String question = null!=jhson?jhson.getString("question"):"";
                String answer = null!=jhson?jhson.getString("answer"):"";

                holder.nick.setText(context.getResources().getString(R.string.notice_group));
                if (name != null && !"".equals(name)) {
//            GlideUtils.loadCircleImage(context, friendHeadEntity.getHeadUrl(), holder.head_image);
                    String uname = messageEntivity.getNick();
                    if (null==uname || TextUtils.isEmpty(uname)){
                        uname = ""+messageEntivity.getDestid() ;
                    }
                    holder.content.setText(uname+"申请加入群组"+name);
                } else {
                    String uname = messageEntivity.getNick();
                    if (null==uname || TextUtils.isEmpty(uname)){
                        uname = ""+messageEntivity.getDestid() ;
                    }
                    holder.content.setText(uname+"申请加入群组");
                }
                holder.content.setVisibility(View.VISIBLE);
                holder.content.setText("验证消息："+userAnswer);

                List<GroupFriendEntivity> groupFriendEntivities = GroupFriendEntivity.find(GroupFriendEntivity.class, "uid = ? and belong_group_id=?", new String[]{ToolsUtils.getMyUserId(), groupId + ""});
                if (groupFriendEntivities.size()>0){
                    String head = groupFriendEntivities.get(0).getHeadUrl();
                    if (head != null && !head.equals("")) {
                        GlideUtils.loadCircleImage(context, head, holder.head_image);
                    }
                }

                holder.agree.setTag(position);
                holder.agree.setOnClickListener(this);
                holder.unagree.setTag(position);
                holder.unagree.setOnClickListener(this);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    @Override
    public int getItemCount() {
        return mGroupEntivities != null ? mGroupEntivities.size() : 0;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        switch (v.getId()){
            case R.id.agree:
                mItemClickListener.onAgreeNewFriendClick(v, position, 2);
                break;
            case R.id.unagree:
                mItemClickListener.onAgreeNewFriendClick(v, position, 1);
                break;
        }

    }
}